<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
        <el-form-item label="用户" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="模型" prop="model">
          <el-select v-model="queryParams.model" placeholder="请选择模型" clearable style="width: 200px">
            <el-option label="GPT-4" value="gpt-4" />
            <el-option label="GPT-3.5" value="gpt-3.5-turbo" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作工具栏 -->
    <el-card class="toolbar-card">
      <el-row :gutter="16" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="Delete"
            :disabled="multiple"
            class="toolbar-button is-danger"
            @click="handleDelete"
          >批量删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="Download"
            class="toolbar-button is-warning"
            @click="handleExport"
          >导出</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 聊天记录表格 -->
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="用户" />
        <el-table-column prop="model" label="AI模型" />
        <el-table-column prop="messageCount" label="消息数" width="100" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template #default="scope">
            <div class="table-operation-wrapper">
              <el-button
                type="primary"
                icon="View"
                @click="handleView(scope.row)"
                class="table-operation-button is-primary"
                size="small"
              >查看</el-button>
              <el-button
                type="danger"
                icon="Delete"
                @click="handleDelete(scope.row)"
                class="table-operation-button is-danger"
                size="small"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)
const total = ref(0)
const ids = ref([])
const multiple = ref(true)
const queryFormRef = ref()

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  username: undefined,
  model: undefined
})

const tableData = ref([
  {
    id: 1,
    username: '张三',
    model: 'GPT-4',
    messageCount: 15,
    createTime: '2024-01-15 10:30:00'
  },
  {
    id: 2,
    username: '李四',
    model: 'GPT-3.5-turbo',
    messageCount: 8,
    createTime: '2024-01-14 14:22:00'
  }
])

// 查询操作
const handleQuery = () => {
  queryParams.pageNum = 1
  fetchData()
}

// 重置操作
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

// 获取表格数据
const fetchData = () => {
  loading.value = true
  // 模拟API请求
  setTimeout(() => {
    total.value = 2
    loading.value = false
  }, 500)
}

// 处理多选
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

// 查看聊天记录详情
const handleView = (row) => {
  ElMessage.info('查看聊天记录: ' + row.id)
}

// 删除聊天记录
const handleDelete = (row) => {
  const chatIds = row?.id ? [row.id] : ids.value
  if (chatIds.length === 0) {
    ElMessage.warning('请选择要删除的记录')
    return
  }
  
  ElMessageBox.confirm('确认删除选中的聊天记录吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    fetchData()
  }).catch(() => {})
}

// 导出聊天记录
const handleExport = () => {
  ElMessage.info('导出功能开发中...')
}

// 处理页面大小变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  fetchData()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style lang="scss" scoped>
.search-card {
  margin-bottom: 16px;
}

.toolbar-card {
  margin-bottom: 16px;
}

.mb8 {
  margin-bottom: 8px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.table-operation-wrapper {
  display: flex !important;
  flex-wrap: wrap !important;
  gap: 5px;
  justify-content: center;
  align-items: center !important;
  margin: -4px !important;
}
</style>
